<?php

namespace App\Http\Controllers\General\File;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\General\File\Traits\UploadTrait;

class LocalUploadController extends Controller
{
    use UploadTrait;

    //
    public function imageUpload(Request $request)
    {
        $image = $request->file('image');
        if (!$image->isValid()) {
            return $this->fail('上传失败，上传文件无效');
        }
        if(!in_array($image->getClientOriginalExtension(),$this->imageTypes())){
            return $this->fail('上传失败，上传文件类型错误');
        }
        $size=round($image->getSize()/1024/1024,2);
        if($size>5){
            return $this->fail('上传失败，上传文件大小不能超过5MB');
        }
        if($request->filled('filename')){
            $relativePath='image' . '/testimg';
            $this->makePublicDirectory($relativePath);
            $path = $image->storeAs($relativePath, $request->filename, 'public');
            $webPath='storage/'.$path;
            return $this->success(['web_path'=>$webPath]);
        }else{
            $relativePath='image' . '/'.date('Ym').'/'.date('d');
            $this->makePublicDirectory($relativePath);
            $path = $image->store($relativePath, 'public');
            $webPath='storage/'.$path;
            if($request->noid != 1){
                $this->createUploadLog($request->user()->id,$image->getClientOriginalName(),'image',$webPath);
            }
            return $this->success(['web_path'=>$webPath]);
        }

    }    //
    public function imagesUpload(Request $request)
    {
        if($request->hasFile('image')){
            // 获取文件名数组
            $namelist = json_decode($request->filenames);
            $webPaths=[];
            foreach ($request->file('image') as $k=>$uploadedFile){
                if ($uploadedFile->isValid()) {
                    $relativePath='image' . '/testimg/product';
                    $this->makePublicDirectory($relativePath);
                    $path = $uploadedFile->storeAs($relativePath, $namelist[$k], 'public');
                    $webPath='storage/'.$path;
                    $webPaths[] = $webPath;
                }
            }
            return $this->success(['web_path'=>json_encode($webPaths)]);
        }else{
            return $this->fail('上传失败，上传文件不存在');
        }

    }

    //
    public function fileUpload(Request $request)
    {
        $file = $request->file('file');
        if (!$file->isValid()) {
            return $this->fail('上传失败，上传文件无效');
        }
        if(!in_array($file->getClientOriginalExtension(),$this->fileTypes())){
            return $this->fail('上传失败，上传文件类型错误');
        }
        $relativePath='file' . '/'.date('Ym').'/'.date('d');
        $this->makePublicDirectory($relativePath);
        $path = $file->store($relativePath, 'public');
        $webPath='storage/'.$path;
        if($request->noid != 1){
            $this->createUploadLog($request->user()->id,$file->getClientOriginalName(),'file',$webPath);
        }
        return $this->success(['web_path'=>$webPath]);
    }



}
